<?php

namespace app\manage\controller;

use app\Request;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;
use app\manage\services\Admin as AdminService;
use app\manage\validate\Admin as AdminValidate;
use think\App;

/**
 * @Apidoc\Title("管理员模块")
 * @Apidoc\Group("")
 */
class Admin extends Base
{
    protected $service;
    protected $validate;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new AdminService();
        $this->validate = new AdminValidate();
    }

    /**
     * @Apidoc\Title("查询分页列表")
     * @Apidoc\Url("/manage/admin/list")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param(ref="pagingParam")
     * @Apidoc\Returned(ref="pagingParam")
     * @Apidoc\Param("user_name",type="varchar",desc="管理员姓名")
     * @Apidoc\Returned("data",type="array",ref="app\model\Admin\getList",withoutField="")
     */
    public function getPageList(Request $request): Json
    {
        $page = $request->param("pageIndex", config('app.defaultPage'));
        $limit = $request->param("pageSize", config('app.defaultLimit'));
        $where = [];
        $name = $request->param("user_name", '');
        if ($name) {
            $where[] = ["user_name", '=', $name];
        }
        $res = $this->service->getPageList($page, $limit, $where);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("根据ID查询明细")
     * @Apidoc\Url("/manage/admin/detail")
     * @Apidoc\Param("id",type="int",require=true,desc="唯一id")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Returned(ref="app\model\Admin\getInfoById",withoutField="")
     */
    public function detail(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('info');
        }
        $params = $request->param();
        $res = $this->service->getInfoById($params['id']);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("新增")
     * @Apidoc\Url("/manage/admin/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="app\model\Admin\getInfoById",withoutField="id,create_at,update_at,status,rid,user_pass,")
     * @Apidoc\Returned (ref="app\model\Admin\getInfoById")
     */
    public function add(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('add');
        }
        $params = $request->post();
        $params['user_pass'] = md5('123456');
        $res = $this->service->add($params);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("编辑")
     * @Apidoc\Url("/manage/admin/edit")
     * @Apidoc\Method("PUT")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param (ref="app\model\Admin\getInfoById",withoutField="create_at,update_at,")
     * @Apidoc\Returned (ref="app\model\Admin\getInfoById")
     */
    public function edit(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('edit');
        }
        $params = $request->post();
        $params['user_pass'] = md5($params['user_pass']);
        $res = $this->service->update($params);
        return jsonT($res);
    }

    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/manage/admin/delete")
     * @Apidoc\Method("DELETE")
     * @Apidoc\Param("id",type="int",require=true,desc="唯一id")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Returned("data",type="boolean",desc="删除状态")
     */
    public function delete(Request $request): Json
    {
        if (!empty($this->validate)) {
            $this->validate->goCheck('delete');
        }
        $params = $request->post();
        $res = $this->service->delete($params['id']);
        return jsonT($res);
    }
}
